/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 14.04.2010
 *     Author: artur
 *
 */

#ifndef CONTROLLER_H_
/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 14.04.2010
 *     Author: artur
 *
 */

#define CONTROLLER_H_

#include <Object.h>
#include <ArchController.h>

#include <kiddie/kiddie.h>
#include <sys/types.h>

namespace kernel
{
	namespace irq
	{

		class Event
		{
			public:
				typedef enum
				{
					EmptyEvent = -1,
					TimerEvent = 6,
					KeyboardEvent = 7,
					Serial1Event = 8,
					Serial2Event = 9,
					HddEvent = 10,
					FpuEvent = 11,
					RtcEvent = 12,
					Ps2Event = 13,
					PageFaultEvent = 14,
					SyscallEvent = 30,
				} EventType;
		};

		class EventListener
		{
			public:
				virtual void onEvent(Event *) = 0;
		};

		/**
		 * EventController is a class that
		 */
		class EventController: public ArchController
		{
			public:
				typedef enum
				{
					OFF = 0, ON, INIT
				} ONOFF;

			public:
				EventController();
				~EventController();
				void init(void);
				EventController &operator<<(ONOFF mode);
				reg_t onInterrupt(struct cpu_regs *regs);
				reg_t onTrap(struct cpu_regs *regs);
				reg_t onSyscall(struct cpu_regs *regs);
				bool AddEventListener(Event::EventType, EventListener *);

				typedef void (*irqfn_t)(struct cpu_regs *r);
				typedef void (*evthdlr_t)(Event *);

				EventController &operator[](kernel::irq::Event::EventType);
				EventController &operator=(kernel::irq::EventListener *);

		private:
		    EventListener *irq_table[IRQ_ENTRIES];
		    EventListener *syscall_table[IRQ_ENTRIES];
		    Event::EventType eventstate;
		};

	}
}

#endif /* CONTROLLER_H_ */
